package com.sam.im.heiqun.uis.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iflytek.cloud.thirdparty.T;
import com.sam.im.heiqun.R;

import static com.sam.im.heiqun.utils.MyDialog.getScreenWidth;

/**
 * Created by zhangqiang on 2018/6/22.
 */
public class ItemSelectorDialog<E extends ItemSelectorDialog.Entity> extends Dialog {

    private final View contentView;
    private CharSequence title;
    private DialogItemClickListener<E>  dialogClickListener;

    public ItemSelectorDialog(@NonNull Context mContext) {
        super(mContext, R.style.DialogStyle);

        contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_radio, null);
        setContentView(contentView);


        contentView.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dismiss();
            }
        });
        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = getScreenWidth(mContext);

        // 贴屏幕底部
        mWindow.setGravity(Gravity.BOTTOM);
        // 添加动画
        mWindow.setWindowAnimations(R.style.dialogAnim);
        mWindow.setAttributes(lp);
        setCancelable(false);
        setCanceledOnTouchOutside(true);
    }


    public void setTitle(CharSequence title){
        this.title = title;

        if (title == null || title.equals("")) {
            contentView.findViewById(R.id.title).setVisibility(View.GONE);
            contentView.findViewById(R.id.title_divider)
                    .setVisibility(View.GONE);
        } else {
            ((TextView) contentView.findViewById(R.id.title)).setText(title);
        }
    }


    public void setItems(final E[] items){

        Context context = getContext();
        // 根据items动态创建
        LinearLayout parent = contentView.findViewById(R.id.dialogLayout);
        parent.removeAllViews();
        int length = items.length;
        for (int i = 0; i < length; i++) {
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(-1, -2);
            params1.rightMargin = 1;
            final TextView tv = new TextView(context);
            tv.setLayoutParams(params1);
            tv.setTextSize(18);
            tv.setText(items[i].getText());
            tv.setTextColor(context.getResources().getColor(
                    R.color.text_color_gray1));
            int pad = context.getResources().getDimensionPixelSize(
                    R.dimen.spacing_default);
            tv.setPadding(pad, pad, pad, pad);
            tv.setSingleLine(true);
            tv.setGravity(Gravity.CENTER);

            if (i != length - 1) {
                if (TextUtils.isEmpty(title) && i == 0) {
                    tv.setBackgroundResource(R.drawable.menudialog_top2_selector);
                } else {
                    tv.setBackgroundResource(R.drawable.menudialog_center_selector);
                }
            } else {
                if (length == 1 && i != 0) {
                    tv.setBackgroundResource(R.drawable.menudialog_bottom_selector);
                } else {
                    tv.setBackgroundResource(R.drawable.menudialog_bottom2_selector);
                }
                if (length == 1 && i == 0) {
                    tv.setBackgroundResource(R.drawable.menudialog_bottom3_selector);
                }
            }
            final int finalI = i;
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    dismiss();
                    dialogClickListener.confirm(finalI,items[finalI]);
                }
            });
            parent.addView(tv);
            if (i != length - 1) {
                TextView divider = new TextView(context);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, (int) 1);
                divider.setLayoutParams(params);
                divider.setBackgroundResource(android.R.color.darker_gray);
                parent.addView(divider);
            }
        }
    }

    public <T extends View> T findViewById(int id){

        return contentView.findViewById(id);
    }

    @Nullable
    public TextView findItemViewWithText(@NonNull CharSequence text){

        LinearLayout parent = contentView.findViewById(R.id.dialogLayout);
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = parent.getChildAt(i);
            if(childAt instanceof TextView){
                if (text.equals(((TextView) childAt).getText())) {
                    return (TextView) childAt;
                }
            }
        }
        return null;
    }

    public interface DialogItemClickListener<E> {
        void confirm(int position,E result);
    }

    public void setDialogClickListener(DialogItemClickListener<E> dialogClickListener) {
        this.dialogClickListener = dialogClickListener;
    }

    public static class Entity{

        private String text;

        public Entity(String text) {
            this.text = text;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }
}
